package com.nxk.modules.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nxk.modules.content.dao.ConProductDao;
import com.nxk.modules.content.entity.ConProduct;
import com.nxk.modules.content.service.ConProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ConProductServiceImpl extends ServiceImpl<ConProductDao, ConProduct> implements ConProductService {

    @Autowired
    private ConProductDao conProductDao;

    /**
     * 保存或更新
     * @param conProduct
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public boolean save(ConProduct conProduct) {
        if (StringUtils.isNotBlank(conProduct.getId())) {
            return super.updateById(conProduct);
        } else {
            return super.save(conProduct);
        }
    }

    /**
     * 分页查询
     * @param conProduct
     * @return
     */
    @Override
    public IPage<ConProduct> page(ConProduct conProduct) {
        Page<ConProduct> page = new Page<ConProduct>(conProduct.getPageNo(), conProduct.getPageSize());
        List<ConProduct> list = conProductDao.findPage(conProduct.getProductName(),
                (conProduct.getPageNo() - 1) * conProduct.getPageSize(),
                conProduct.getPageSize());
        page.setRecords(list);
        page.setTotal(conProductDao.findCount(conProduct.getProductName()));
        page.setCurrent(conProduct.getPageNo());
        page.setSize(conProduct.getPageSize());
        return page;
    }

    /**
     * 根据id,物理删除
     * @param id
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public boolean deleteById(String id) {
        return super.removeById(id);
    }



    /**
     * 根据名称模糊查询,统计数量
     * @param productName
     * @return
     */
    @Override
    public long findCount(String productName) {
        return conProductDao.findCount(productName);
    }

    /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
    @Override
    public ConProduct getOne(String id) {
        return conProductDao.getOne(id);
    }

}
